//
//  MGTableViewRadiusManage.m
//  MGPublic
//
//  Created by lhongl on 2025/10/23.
//

#import "MGTableViewRadiusManage.h"

@implementation MGTableViewRadiusManage
- (instancetype)initTableViewManageWithTableView:(__kindof UITableView *)tableView;{
    if (self = [super initTableViewManageWithTableView:tableView]) {
        [self setDefaultInit];
    }
    return self;
}

#pragma mark 设置初始值
- (void)setDefaultInit{
    self.radius = 6;
    self.fillColor = [UIColor whiteColor];
}

#pragma mark 代理
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{

    cell.backgroundColor = UIColor.clearColor;

    CAShapeLayer *layer = [[CAShapeLayer alloc] init];

    CGRect bounds = CGRectInset(cell.bounds, 0, 0);

    UIBezierPath  *path;

    if ([tableView numberOfRowsInSection:indexPath.section]-1 == 0) {

       path = [UIBezierPath bezierPathWithRoundedRect:bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(self.radius, self.radius)];

    }else if (indexPath.row == 0) {

        path = [UIBezierPath bezierPathWithRoundedRect:bounds byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight cornerRadii:CGSizeMake(self.radius, self.radius)];

    } else if (indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-1) {

        path = [UIBezierPath bezierPathWithRoundedRect:bounds byRoundingCorners:UIRectCornerBottomLeft|UIRectCornerBottomRight cornerRadii:CGSizeMake(self.radius, self.radius)];
    } else {

        path = [UIBezierPath bezierPathWithRect:bounds];

    }

    layer.path = path.CGPath;

    layer.fillColor = self.fillColor.CGColor;

    UIView *roundView = [[UIView alloc] initWithFrame:bounds];

    [roundView.layer insertSublayer:layer atIndex:0];
    roundView.backgroundColor = UIColor.clearColor;
    cell.backgroundView = roundView;
}


@end
